//
//  MainVC.swift
//  NewDelivery
//
//  Created by 天下宅 on 2016/11/7.
//  Copyright © 2016年 天下宅. All rights reserved.
//

import UIKit
import Kingfisher




class MainVC: BaseVC {

    

    
    
    let leftView = UIView()
    let centerView = UIView()
    
    let coverView = UIView()
    
    let headIV = UIImageView()
    let phoneLabel = UILabel()
    
    let leftTableView = UITableView()
    let centerTableView = UITableView()
    
    
    var leftTVPresenter:MainLeftTVPresenter?
    var centerTVPresenter:MainCenterTVPresenter?
    
    var startDateBtn = DateBtn.init(withTitle: "起始日期：")
    var endDateBtn = DateBtn.init(withTitle: "截至日期：")

    var searchBtn  = UIButton.init(type: UIButtonType.custom)
    
    
    var navView:WKNavView!
    
    var status = false //false 为正常模式 true 为侧滑模式
    {
        didSet{
            
            coverView.isHidden = !status
            let alpha:CGFloat = status ? 0.05 : 0//就像没出现一样  欺骗眼睛
            UIView.animate(withDuration: 0.3, animations: {
                self.centerView.left = self.status ? self.leftView.right : 0
                self.coverView.alpha = alpha
            })

        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        centerTVPresenter?.orderState =  centerTVPresenter?.orderState ?? .new
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.creatLeftView()
        self.creatCenterView()
        
        coverView.frame = CGRect.init(x: screenWidth * 0.8, y: 0, width: screenWidth * 0.2, height: screenHeight)
        coverView.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        coverView.isHidden = true
        coverView.alpha = 0
        coverView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(coverClick))
        coverView.addGestureRecognizer(tap)
        self.view.addSubview(coverView)
        // Do any additional setup after loading the view.
    }

    func coverClick()
    {
        status = false
        print("123")
    }
    
    func creatLeftView()
    {
        leftView.frame = CGRect.init(x: 0, y: 0, width: screenWidth * 0.8, height: screenHeight)
        leftView.backgroundColor = UIColor.darkGray
        self.view.addSubview(leftView)
        
        headIV.backgroundColor = UIColor.brown
        headIV.layer.cornerRadius = 60
        headIV.layer.masksToBounds = true
        leftView.addSubview(headIV)
        headIV.snp.makeConstraints { (make) in
            make.top.equalTo(leftView).offset(40)
            make.centerX.equalToSuperview()
            make.width.height.equalTo(120)
        }
        headIV.image = UIImage.init(named: "diliveryManImage")
        
        let userInfo = WKUserInfo.shared
        if userInfo.logoURL != nil
        {
            let imgResource = ImageResource.init(downloadURL: userInfo.logoURL!)
            headIV.kf.setImage(with: imgResource)
        }

        
        phoneLabel.text = userInfo.phone
        phoneLabel.textColor = UIColor.lightText
        leftView.addSubview(phoneLabel)
        phoneLabel.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(headIV.snp.bottom).offset(20)
            make.height.equalTo(20)
        }
        phoneLabel.textAlignment = .center
        
        leftView.addSubview(leftTableView)
        leftTableView.backgroundColor = UIColor.darkGray
        leftTableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(phoneLabel.snp.bottom).offset(20)
        }
        leftTVPresenter = MainLeftTVPresenter.init(WithTV: leftTableView, tvc: self)
        
    }
    
    func creatCenterView()  {
        centerView.backgroundColor = UIColor.groupTableViewBackground
        centerView.frame = self.view.frame
        self.view.addSubview(centerView)
        
        navView = WKNavView.creatNavView(WithTitle: "待接订单", vProtocol: self)
        centerView.addSubview(navView)
        navView.leftImage = UIImage.init(named: "菜单栏")
        
        let sevenDayAgo = String.timeStampToString(timeS: Date.init(timeIntervalSinceNow: -(24*60*60*7)).timeIntervalSince1970 * 1000, dateStyle: "yyyy-MM-dd")
        centerView.addSubview(startDateBtn)
        startDateBtn.date = sevenDayAgo
        startDateBtn.snp.makeConstraints { (make) in
            make.left.equalTo(centerView).offset(20)
            make.top.equalTo(navView.snp.bottom)
            make.height.equalTo(30)
            make.width.equalTo(screenWidth - 60)
        }
        let now = String.timeStampToString(timeS: Date().timeIntervalSince1970 * 1000, dateStyle: "yyyy-MM-dd")
        endDateBtn.date = now;
        centerView.addSubview(endDateBtn)
        endDateBtn.snp.makeConstraints { (make) in
            make.left.equalTo(centerView).offset(20)
            make.top.equalTo(startDateBtn.snp.bottom)
            make.height.equalTo(30)
            make.width.equalTo(screenWidth - 60)
        }
        
        centerView.addSubview(searchBtn)
        searchBtn.snp.makeConstraints { (make) in
            make.top.equalTo(navView.snp.bottom).offset(5)
            make.right.equalTo(centerView).offset(-10)
            make.width.equalTo(60)
            make.bottom.equalTo(endDateBtn).offset(-5)
        }
        searchBtn.setTitleColor(UIColor.white, for: UIControlState.normal)
        searchBtn.setTitle("搜索", for: UIControlState.normal)
        searchBtn.backgroundColor = MainColor
        searchBtn.addTarget(self, action: #selector(startSearch), for: .touchUpInside)
        searchBtn.layer.cornerRadius = 5
        
        centerView.addSubview(centerTableView)
        centerTableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(navView.snp.bottom)
        }
        

        
        centerTVPresenter = MainCenterTVPresenter.init(WithTV: centerTableView, tvc: self)
        centerTVPresenter?.orderState = .new
        centerTVPresenter?.isRefresh = true
        centerTVPresenter?.isLoadMore = true

    }
    
    func startSearch()  {
        
        let f = DateFormatter()
        f.dateFormat = "yyyy-MM-dd"
        let start = f.date(from: startDateBtn.date) ?? Date()
        let end = f.date(from: endDateBtn.date) ?? Date()
        if start.timeIntervalSince1970 > end.timeIntervalSince1970  {
            self.view.wkMakeToastToCenter(message: "截止时间应大于起始时间");
            return;
        }
        
        centerTVPresenter?.startDate = startDateBtn.date
        centerTVPresenter?.endDate = endDateBtn.date
        centerTVPresenter?.refresh()
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func navBtnClick(WithType type: WKNavViewClickType) {
        status = true
    }
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
